Ext.onReady(function(){

	
    var bbsRec = Ext.data.Record.create([
    {
        name: 'author',
        type: 'string'
    }, {
        name: 'title',
        type: 'string'
    }, {
        name: 'count',
        type: 'string'
    }]);
    
    var proxy = new Ext.data.HttpProxy({
 	   url: 'bbs/view.htm'
    });
    var reader = new Ext.data.JsonReader({
        totalProperty: 'totalCount',
        root: 'datas',
        fields        : [{name:'author',	mapping:'author'},
        				{name:'title',		mapping:'title'},
        				{name:'count',		mapping:'count'}]
    });

 // The new DataWriter component.
    var writer = new Ext.data.JsonWriter({
        encode: true,
        writeAllFields: false
    });
    
 // Typical Store collecting the Proxy, Reader and Writer together.
    var store = new Ext.data.Store({
        id: 'bbsStore',
        proxy: proxy,
        reader: reader,
        writer: writer,  // <-- plug a DataWriter into the store just as you would a Reader
        autoSave: false // <-- false would delay executing create, update, destroy requests until specifically told to do so with some [save] buton.
    });

    //read the data from simple array
    store.load();
//    
    Ext.data.DataProxy.addListener('exception', function(proxy, type, action, options, response, arg ) {
    	 Ext.Msg.show({
             title: 'REMOTE EXCEPTION',
             msg: response.message,
             icon: Ext.MessageBox.ERROR,
             buttons: Ext.Msg.OK
         });
    });
    var cm = new Ext.grid.ColumnModel({
        defaults: {
            sortable: true            
        },
        columns: [new Ext.grid.RowNumberer({}),
        	{
                header    : "글쓴이",
                dataIndex : 'author',
                width     : 50,
                align     : 'center',
                sortable  : true
            },{
            	header    : "제목",
                dataIndex : 'title',
                width     : 50,
                align     : 'center',
                sortable  : true
            },{
            	header	  : "조회수",
            	dataIndex : 'count',
            	width	  : 50,
            	align     : 'center'
            }
            ]
    });
    

    // create grid
    var grid = new Ext.grid.GridPanel({
        store: store,
        cm	 : cm,
        title: 'BBS',
        height: 300,
        width:610,
		frame:true,
		tbar: [{
            iconCls: 'icon-user-add',
            text: 'Write',
            handler: function(){
				var w = new Ext.Window({
					height: 400, width: 600,
					items: form
				});
				w.show();
            }
        }]
    });
    
    var form = new Ext.FormPanel({
        labelAlign: 'top',
        frame:true,
        title: 'BBS Write',
        bodyStyle:'padding:5px 5px 0',
        width: 600,
        items: [{
            layout:'column',
            items:[{
                layout: 'form',
                items: [{
                	id:'title',
                    xtype:'textfield',
                    fieldLabel: 'Title',
                    name: 'title',
                    anchor:'95%'
                }]
            }]
        },{
            xtype:'htmleditor',
            id:'data',
            fieldLabel:'Data',
            height:200,
            anchor:'98%'
        }],
        buttons: [{
            text: 'Save',
            handler: function(){
        		alert("Save");
        		var array = new Array();
        		var a = "aaa";
        		var b = "bbb";
        		array.push(a);
        		array.push(b);
        		var c = Ext.util.JSON.encode(array);
        		alert(c);
        		var req = Ext.Ajax.request({
        		    url: "/bbs/write.htm",
        		    method  : 'POST',
        			waitMsg : 'Loading',
        			params  : {"title" : form.findById('title').getValue(),
        						"data" : form.findById('data').getValue()},
        		    disableCaching: false
        		});
        	}
        },{
            text: 'Cancel',
            handler: function(){
    			alert("Cancel");
    		}
        }]
    });
    
    grid.render('bbsdiv');
});